<template>
  <div class="emoji-cipher">
    <div class="header">
      <h1>🔐 Emoji密码本 🔐</h1>
      <p>安全有趣的加密聊天工具</p>
    </div>

    <div class="encrypt-section">
      <h2>加密文字</h2>
      <div class="input-container">
        <input 
          v-model="inputText" 
          type="text" 
          placeholder="请输入要加密的文字,我爱你"
          @focus="inputFocused = true"
          @blur="inputFocused = false"
          :class="{ 'focused': inputFocused }"
        />
        <button 
          @click="encrypt" 
          :disabled="isProcessing"
          :class="{ 'processing': isProcessing }"
        >
          生成emoji密码
        </button>
      </div>

      <div v-if="encryptedEmoji" class="result-card">
        <h3>加密结果</h3>
        <div class="emoji-result">{{ encryptedEmoji }}</div>
        <button @click="copyToClipboard(encryptedEmoji)" class="copy-btn">
          复制密码
        </button>
      </div>
    </div>

    <div class="divider"></div>

    <div class="decrypt-section">
      <h2>解密密码</h2>
      <div class="input-container">
        <input 
          v-model="emojiInput" 
          type="text" 
          placeholder="请输入emoji密码"
          @focus="emojiInputFocused = true"
          @blur="emojiInputFocused = false"
          :class="{ 'focused': emojiInputFocused }"
        />
        <button 
          @click="decrypt" 
          :disabled="isProcessing"
          :class="{ 'processing': isProcessing }"
        >
          解密emoji密码
        </button>
      </div>

      <div v-if="decryptedText" class="result-card">
        <h3>解密结果</h3>
        <div class="text-result">{{ decryptedText }}</div>
        <button @click="copyToClipboard(decryptedText)" class="copy-btn">
          复制文字
        </button>
      </div>

      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
    </div>
  </div>
</template>

<script>
// 导入加解密函数
import { encryptToEmoji, decryptFromEmoji } from './emoj.js'

export default {
  name: 'EmojiCipher',
  data() {
    return {
      // 用户输入的原始文本
inputText: '',
// 加密后的emoji结果
encryptedEmoji: '',
// 用户输入的emoji密码
emojiInput: '',
// 解密后的文本结果
decryptedText: '',
// 错误提示信息
errorMessage: '',
// 是否正在处理中
isProcessing: false,
// 输入框是否获得焦点
inputFocused: false,
// emoji输入框是否获得焦点
emojiInputFocused: false    }
  },
  methods: {
    // 加密文本为emoji密码
    encrypt() {
      if (!this.inputText.trim()) {
        this.errorMessage = '请输入要加密的文字'
        return
      }
      
      this.errorMessage = ''
      this.isProcessing = true
      
      // 模拟处理延迟，增强用户体验
      setTimeout(() => {
        try {
          this.encryptedEmoji = encryptToEmoji(this.inputText)
        } catch (error) {
          this.errorMessage = '加密失败，请重试'
          console.error('加密错误:', error)
        } finally {
          this.isProcessing = false
        }
      }, 300)
    },
    
    // 解密emoji密码为原文
    decrypt() {
      if (!this.emojiInput.trim()) {
        this.errorMessage = '请输入要解密的emoji密码'
        return
      }
      
      this.errorMessage = ''
      this.isProcessing = true
      
      // 模拟处理延迟，增强用户体验
      setTimeout(() => {
        try {
          this.decryptedText = decryptFromEmoji(this.emojiInput)
          
          // 检查解密结果是否包含未知Emoji
          if (this.decryptedText.includes('[未知Emoji:')) {
            this.errorMessage = '部分emoji无法识别，请检查输入'
          }
        } catch (error) {
          this.errorMessage = '无法识别该emoji密码，请检查后重新输入'
          console.error('解密错误:', error)
        } finally {
          this.isProcessing = false
        }
      }, 300)
    },
    
    // 复制文本到剪贴板
    copyToClipboard(text) {
      navigator.clipboard.writeText(text)
        .then(() => {
          // 可以添加复制成功的提示
          this.$message ? this.$message.success('复制成功') : alert('复制成功')
        })
        .catch(err => {
          console.error('复制失败:', err)
          this.$message ? this.$message.error('复制失败') : alert('复制失败')
        })
    }
  }
}
</script>

<style scoped>
.emoji-cipher {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Arial', sans-serif;
  color: #333;
}

.header {
  text-align: center;
  margin-bottom: 30px;
}

.header h1 {
  font-size: 28px;
  margin-bottom: 10px;
  background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: glow 2s ease-in-out infinite alternate;
}

.header p {
  color: #666;
  font-size: 16px;
}

.encrypt-section, .decrypt-section {
  background-color: #fff;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.encrypt-section:hover, .decrypt-section:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}

h2 {
  font-size: 20px;
  margin-bottom: 15px;
  color: #444;
}

.input-container {
  display: flex;
  margin-bottom: 20px;
}

input {
  flex: 1;
  padding: 12px 15px;
  border: 2px solid #ddd;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s ease;
  margin-right: 10px;
}

input:focus {
  outline: none;
  border-color: #4ecdc4;
  box-shadow: 0 0 8px rgba(78, 205, 196, 0.4);
}

input.focused {
  transform: scale(1.02);
  border-color: #4ecdc4;
}

button {
  background: linear-gradient(45deg, #ff6b6b, #ff8e53);
  border: none;
  color: white;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

button:before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: rgba(255, 255, 255, 0.2);
  transform: rotate(45deg);
  animation: shine 3s infinite;
}

button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(255, 107, 107, 0.3);
}

button:active {
  transform: translateY(1px);
}

button.processing {
  opacity: 0.7;
  cursor: not-allowed;
}

.result-card {
  background-color: #f9f9f9;
  border-radius: 8px;
  padding: 15px;
  margin-top: 20px;
  position: relative;
  overflow: hidden;
}

.result-card:before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #ff6b6b, #4ecdc4, #ff6b6b);
  background-size: 200% 100%;
  animation: gradient 3s ease infinite;
}

.result-card h3 {
  font-size: 16px;
  margin-bottom: 10px;
  color: #555;
}

.emoji-result {
  font-size: 24px;
  padding: 10px;
  background-color: #fff;
  border-radius: 6px;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
  min-height: 40px;
  margin-bottom: 15px;
  word-break: break-all;
}

.text-result {
  font-size: 18px;
  padding: 10px;
  background-color: #fff;
  border-radius: 6px;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1);
  min-height: 40px;
  margin-bottom: 15px;
  word-break: break-all;
}

.copy-btn {
  background: linear-gradient(45deg, #4ecdc4, #556270);
  font-size: 14px;
  padding: 8px 15px;
}

.divider {
  height: 2px;
  background: linear-gradient(90deg, transparent, rgba(78, 205, 196, 0.5), transparent);
  margin: 30px 0;
}

.error-message {
  color: #ff6b6b;
  margin-top: 10px;
  font-size: 14px;
  text-align: center;
  padding: 8px;
  background-color: rgba(255, 107, 107, 0.1);
  border-radius: 6px;
}

@keyframes shine {
  0% {
    left: -100%;
    opacity: 0;
  }
  20% {
    left: 100%;
    opacity: 0.5;
  }
  100% {
    left: 100%;
    opacity: 0;
  }
}

@keyframes gradient {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

@keyframes glow {
  from {
    text-shadow: 0 0 5px rgba(78, 205, 196, 0.5);
  }
  to {
    text-shadow: 0 0 15px rgba(255, 107, 107, 0.8);
  }
}

/* 移动端适配 */
@media (max-width: 600px) {
  .input-container {
    flex-direction: column;
  }
  
  input {
    margin-right: 0;
    margin-bottom: 10px;
  }
  
  .emoji-result, .text-result {
    font-size: 18px;
  }
  
  .header h1 {
    font-size: 24px;
  }
}
</style>